define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/clock/index' + location.search,
                    //add_url: 'shop/clock/add',
                    //edit_url: 'shop/clock/edit',
                    del_url: 'shop/clock/del',
                    //multi_url: 'shop/clock/multi',
                    //import_url: 'shop/clock/import',
                    table: 'shop/clock',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='user.nickname']", form).addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "nickname")
                    .data("field", "nickname")
                    .data("orderBy", "id asc");
                $("input[name='shop.name']", form).addClass("selectpage")
                    .data("source", "shop/shop/index")
                    .data("primaryKey", "name")
                    .data("field", "name")
                    .data("orderBy", "id asc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'shop.name', title: __('Shop_id')},
                        {field: 'user.nickname', title: __('User_id')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        shop_clock_rank: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/clock/shop_clock_rank' + location.search,
                    table: 'shop/clock',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='createtime']", form).addClass("datetimepicker")
                    .data("date-format", "YYYY-MM");

                $("input[name='user.nickname']", form).addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "nickname")
                    .data("field", "nickname")
                    .data("orderBy", "id asc");
                $("input[name='shop.name']", form).addClass("selectpage")
                    .data("source", "shop/shop/index")
                    .data("primaryKey", "name")
                    .data("field", "name")
                    .data("orderBy", "id asc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                pagination: false,
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'createtime', title: __('Createtime'), visible: false},
                        {field: 'rank', title: __('排名'), operate: false, sortable: true},
                        {field: 'name', title: __('Shop_id'), operate: false},
                        {field: 'clock_number', title: __('打卡次数'), operate: false, sortable: true},
                        {field: 'max_time', title: __('最后打卡时间'), operate: false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});